using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class SelectData
{
    public List<List<int>> numbers;

    public override string ToString()
    {
        StringBuilder result = new StringBuilder();
        int index = 0;
        foreach (List<int> number in numbers)
        {
            index++;
            result.Append(string.Join("",number.ToArray()));
            if (index < numbers.Count)
            {
                result.Append("-");
            }
        }
        return result.ToString();
    }
}

[UIAttr("SelectPanel")]
public class SelectPanel : PanelSingleton<SelectPanel>
{
    public static bool IsShow { get; set; }
    Button ConfirmBtn;
    Button CancelBtn;

    public class Node
    {
        public Toggle tg;
        public Text Label;
    }
    public class GroupNodes
    {
        public Node[] nodes = new Node[10];
    }
    GroupNodes[] groupNodes = new GroupNodes[3];
    private void Start()
    {
        ConfirmBtn = FindNode(gameObject, "Confirm").GetComponent<Button>();
        CancelBtn = FindNode(gameObject, "Cancel").GetComponent<Button>();
        ConfirmBtn.onClick.AddListener(() => { ConfirmAction(); });
  

        CancelBtn.onClick.AddListener(() =>
        {

            this.Close();
        });
        for (int i = 0; i < 3; i++)
        {
            GameObject group = FindNode(gameObject, "zu" + i);
            groupNodes[i] = new GroupNodes();
            for (int j = 0; j < 10; j++)
            {
                var child = group.transform.GetChild(j).gameObject;
                groupNodes[i].nodes[j] = new Node();
                groupNodes[i].nodes[j].tg = child.GetComponent<Toggle>();
                groupNodes[i].nodes[j].tg.isOn = false;
                groupNodes[i].nodes[j].Label = FindNode(child, "Label").GetComponent<Text>();
                groupNodes[i].nodes[j].Label.text = j.ToString();
            }
        }
    }

    void InitUI()
    {

    }
    public override void Open()
    {
        base.Open();
        CurrentData = null;
        IsShow = true;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                groupNodes[i].nodes[j].tg.isOn = false;
            }
        }
    }
    public static SelectData CurrentData;
    async UniTask ConfirmAction()
    {
        CurrentData = new SelectData();
        CurrentData.numbers = new List<List<int>>();
        for (int i = 0; i < 3; i++)
        {
            CurrentData.numbers.Add(new List<int>());
            for (int j = 0; j < 10; j++)
            {
                if (groupNodes[i].nodes[j].tg.isOn)
                    CurrentData.numbers[i].Add(j);
            }
        }

        Close();
    }

    public override void Close()
    {
        base.Close();
        IsShow = false;
    }
}
